package com.aigou.controller;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aigou.exception.AigouException;
import com.aigou.pojo.Content;
import com.aigou.pojo.Result;
import com.aigou.service.IContentService;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cw
 * @since 2024-05-11
 */
@Controller
@RequestMapping("/content")
public class ContentController {
	
	@Autowired
	IContentService contentService;
	@Autowired
	RedisTemplate redisTemplate;

	@RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ResponseBody
    public Result findAll(Content brand) {

        Result result = new Result();
        result.setData(contentService.list());
        return result;
    }
    
    @PostMapping(value = "/add")
    @ResponseBody
    public Result add(@RequestBody Content content) {
    	
//    	if (StringUtils.isEmpty(content.getUrl())) {
//			throw new AigouException("路径不能为空");
//		}
    	
    	contentService.save(content);
    	
    	redisTemplate.delete("content:" + content.getId());
    	
    	Result result = new Result();
    	return result;
    }
    
    @PutMapping(value = "/update")
    @ResponseBody
    public Result update(@RequestBody Content content) {
    	
    	contentService.updateById(content);
    	
    	// redisTemplate.delete("content"); // 删除所有content
    	// 清除缓存
    	redisTemplate.opsForHash().delete("content", content.getId());
    	
    	Result result = new Result();
    	return result;
    }
    
    @DeleteMapping(value = "/delete/{id}")
    @ResponseBody
    public Result delete(@PathVariable("id") Long id) {
    	
    	// 清除缓存
    	redisTemplate.opsForHash().delete("content", id);
    	Result result = new Result();
    
    	return result;
    }
    
    @GetMapping(value = "/findOne/{id}")
    @ResponseBody
    public Result findOne(@PathVariable("id") Long id) {
    	Result result = new Result();
    	Content content = null;
    	// 先查询缓存
    	content = (Content) redisTemplate.opsForValue().get("content:" + id);
//    	content = (Content) redisTemplate.opsForHash().get("content", id);
    	
    	// 缓存穿透
    	// 如果有就返回
    	if (content == null) {
    		// 否则查询数据库
        	content = contentService.getById(id);
        	
        	if (content == null) {
        		redisTemplate.opsForValue().set("content:" + id, new Content(), 20, TimeUnit.SECONDS);
			} else {
				// 放入缓存
	        	 redisTemplate.opsForValue().set("content:" + id, content);
//	        	redisTemplate.opsForHash().put("content", id, content);
			}
        	
		}
    	result.setData(content);
    	return result;
    }
	
}
